﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Button = System.Windows.Forms.Button;

namespace shptxt
{
    public partial class Form1 : Form
    {
        GISLayer layer = null;
        GISView view = null;
        public Form1()
        {
            InitializeComponent();
            view = new GISView(new GISExtent(new GISVertex(0, 0), new GISVertex(100, 100)),
                    ClientRectangle);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Shapefile文件|*.shp";
            openFileDialog.RestoreDirectory = false;
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() != DialogResult.OK) return;
            layer = GISShapefile.ReadShapeFile(openFileDialog.FileName);
            if (checkBox2.Checked)
            {
                layer.DrawAttributeOrNot = true;
            }
            else
            {
                layer.DrawAttributeOrNot = false;
            }
            if (checkBox1.Checked)
            {
                GISTXT.Exporttxt(layer,openFileDialog.FileName);
                MessageBox.Show("输出成功");
            }
            MessageBox.Show("read " + layer.FeatureCount() + " objects.");
            view.UpdateExtent(layer.Extent);
            UpdateMap();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            view.UpdateExtent(layer.Extent);
            UpdateMap();
        }

        private void UpdateMap()
        {
            Graphics graphics = CreateGraphics();
            graphics.FillRectangle(new SolidBrush(Color.Black), ClientRectangle);
            layer.draw(graphics, view);
        }

        private void MapButtonClick(object sender, EventArgs e)
        {
            GISMapActions action = GISMapActions.zoomin;
            if ((Button)sender == button3) action = GISMapActions.zoomin;
            else if ((Button)sender == button4) action = GISMapActions.zoomout;
            else if ((Button)sender == button5) action = GISMapActions.moveup;
            else if ((Button)sender == button6) action = GISMapActions.movedown;
            else if ((Button)sender == button7) action = GISMapActions.moveleft;
            else if ((Button)sender == button8) action = GISMapActions.moveright;
            view.ChangeView(action);
            UpdateMap();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(layer);
            form2.Show();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog2 = new OpenFileDialog();
            openFileDialog2.Filter = "TXT文件|*.txt";
            openFileDialog2.RestoreDirectory = false;
            openFileDialog2.FilterIndex = 1;
            openFileDialog2.Multiselect = false;
            if (openFileDialog2.ShowDialog() != DialogResult.OK) return;
            layer = GISTXT.ReadTXT(openFileDialog2.FileName,openFileDialog2.SafeFileName);
            if (checkBox2.Checked) 
            {
                layer.DrawAttributeOrNot = true;
            }
            else
            {
                layer.DrawAttributeOrNot = false;
            }
             //不绘制属性
            MessageBox.Show("read " + layer.FeatureCount() + " objects.");
            view.UpdateExtent(layer.Extent);
            UpdateMap();
        }
    }
}
